<?php
function writeDownloadSection($subdir, $filebase, $filetype)
{
	$count = 0;
	if ($dh = opendir($subdir)) {
		while ($file = readdir($dh)) {
			$pieces = explode('-', $file);
			if ($pieces[0] == $filebase) {
				++$count;
				if ($count == 1) {
					echo "<table border=0 cellpadding=0 cellspacing=0>\n";
					echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>\n";
					echo "  <table border=0 cellpadding=5 cellspacing=2>\n";
				}

				echo "    <tr bgcolor=#dddddd>\n";
				echo "      <td><a href=\"$subdir/$file\">$filetype for ";

				if ($pieces[1] == 'win') {
					echo 'Windows';
				}
				else if ($pieces[1] == 'osx') {
					echo 'OS X';
				}
				else {
					echo 'UNKNOWN PLATFORM';
				}

				echo "</a></td>\n";

				if ($subdir != 'video') {
					$vparts = explode('.', $pieces[2]);
					$ext = array_pop($vparts);
					$version = implode('.', $vparts);
					echo "      <td>v$version</td>\n";

					echo '      <td>', strtoupper($ext), " format</td>\n";
				}

				$finfo = stat("$subdir/$file");
				echo '      <td>updated ', strftime('%Y.%m.%d', $finfo['mtime']),
						"</td>\n";
				
				echo '      <td>';
				$size = $finfo['size'];
				if ($size > 1024) {
					$size /= 1024.0;
					if ($size > 1024) {
						$size /= 1024.0;
						if ($size > 1024) {
							printf("%.1f GB", $size);
						}
						else {
							printf("%.1f MB", $size);
						}
					}
					else {
						printf("%.1f KB", $size);
					}
				}
				else {
					echo "$size bytes</td>\n";
				}

				echo "    </tr>\n";
			}
		}

		closedir($dh);
	}

	if ($count) {
		echo "</td></tr></table></table>\n";
	}
	else {
		echo '<p>No files available.</p>';
	}
}
?>

<html>
<head>
	<meta http-equiv="content-type" content="text/html;charset=utf-8">
	<title><?php echo $title; ?></title>
	<link href="/root.css" rel="Stylesheet" type="text/css">
</head>

<body>
		<table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#9aa1a7">
			<tr height="100%">
				<td rowspan="3" width="1%"><img src="/bitmaps/top-box-left.png" alt="" height="98" width="11" border="0"></td>
				<td rowspan="3" width="1%"><a href="http://wickdd.org/"><img src="/bitmaps/logo.png" alt="" height="77" width="80" border="0"></a></td>
				<td width="20">&nbsp;</td>
				<td rowspan="2" valign="bottom"><a href="/"><img src="/bitmaps/site-title.png" alt="" border="0" vspace="8"></a></td>
				<td rowspan="3" width="1%"><img src="/bitmaps/top-box-right.png" alt="" height="98" width="11" border="0"></td>
			</tr>
			<tr>
				<td width="20">&nbsp;</td>
			</tr>
			<tr height="26">
				<td width="20" height="26">&nbsp;</td>
				<td align="right" valign="bottom" height="26">
					<img src="/bitmaps/navbar-left-cap.png" alt="" border="0"><a href="/userdoc.html"><img src="/bitmaps/navbar-docs.png" alt="" border="0"></a><img src="/bitmaps/navbar-separator.png" alt="" border="0"><a href="/lists.html"><img src="/bitmaps/navbar-lists.png" alt="" border="0"></a><img src="/bitmaps/navbar-separator.png" alt="" border="0"><a href="/hacking.html"><img src="/bitmaps/navbar-hacking.png" alt="" border="0"></a><img src="/bitmaps/navbar-separator.png" alt="" border="0"><a href="/licenses.html"><img src="/bitmaps/navbar-licenses.png" alt="" border="0"></a><img src="/bitmaps/navbar-right-cap.png" alt="" border="0">
				</td>
			</tr>
		</table>
		
		<p></p>
		
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr height="21">
				<td width="25" height="21"><img src="/bitmaps/body-top-left.png" alt="" height="21" width="25" border="0"></td>
				<td height="21" background="/bitmaps/body-top-middle.png"></td>
				<td width="25" height="21"><img src="/bitmaps/body-top-right.png" alt="" height="21" width="25" border="0"></td>
			</tr>
			<tr>
				<td width="25" background="/bitmaps/body-side-left.png"></td>
				<td bgcolor="white">
					<h1>Released Versions</h1>
					<?php 
						writeDownloadSection('release', $filebase,
								$appver . ' Clients');

						if (file_exists('release/ChangeLog')) {
							echo '<p><a href="release/ChangeLog">Latest change log</a></p>';
						}
					?> 

					<h1>Test Versions</h1>
					<?php writeDownloadSection('test', $filebase,
							$appver . ' Clients'); ?> 

					<h1>Tutorial Videos</h1>
					<?php writeDownloadSection('video', $filebase,
							$appver . ' Intro Video'); ?> 

					<h1>Source Code</h1>
					<p><?php echo $sourceloc; ?></p>
				</td>
				<td width="25" background="/bitmaps/body-side-right.png"></td>
			</tr>
			<tr height="21">
				<td width="25" height="24"><img src="/bitmaps/body-bottom-left.png" alt="" height="24" width="25" border="0"></td>
				<td height="24" background="/bitmaps/body-bottom-middle.png"></td>
				<td width="25" height="24"><img src="/bitmaps/body-bottom-right.png" alt="" height="24" width="25" border="0"></td>
			</tr>
		</table>
		<p></p>
	</body>
</html>
